package com.africanmoviesmobile.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.africanmoviesmobile.R;
import com.africanmoviesmobile.ui.HomeActivity;
import com.africanmoviesmobile.ui.MovieDetailActivity;
import com.africanmoviesmobile.util.bitmap.ImageWorker;


public class LatestMoviesFragment extends Fragment {
	
	private static final String EXTRA_MOVIE_ID = "movie_id";
	private static final String EXTRA_MOVIE_COVER = "movie_cover";
	private static final String EXTRA_MOBILE_SCREENSHOT = "mobile_screenshot";
	
    private ImageView mMovieCoverImageView;
    private ImageView mMobileScreenshotImageView;
    
    private int mMovieId;
    private String mMovieCoverUrl;
    private String mMobileScreenshotUrl;

    /**
     * Factory method for this fragment class.
     */
    public static LatestMoviesFragment newInstance(int movieId, String movieCoverUrl, String mobileScreenshotUrl) {
    	
    	LatestMoviesFragment fragment = new LatestMoviesFragment();
        
    	Bundle args = new Bundle();
        args.putInt(EXTRA_MOVIE_ID, movieId);
        args.putString(EXTRA_MOVIE_COVER, movieCoverUrl);
        args.putString(EXTRA_MOBILE_SCREENSHOT, mobileScreenshotUrl);
        
        fragment.setArguments(args);
        
        return fragment;
    }

    public LatestMoviesFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMovieId = getArguments().getInt(EXTRA_MOVIE_ID);
        mMovieCoverUrl = getArguments().getString(EXTRA_MOVIE_COVER);
        mMobileScreenshotUrl = getArguments().getString(EXTRA_MOBILE_SCREENSHOT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.latest_movies_page_view, container, false);
        
        mMovieCoverImageView = (ImageView) rootView.findViewById(R.id.movie_cover);
        mMovieCoverImageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				final Intent intent = new Intent(getActivity(), MovieDetailActivity.class);
	    		intent.putExtra(MovieDetailActivity.EXTRA_MOVIE_ID, mMovieId);
	            startActivity(intent);
			}
		});
        
        ImageView playTrailerButton = (ImageView) rootView.findViewById(R.id.play_trailer_button);
        playTrailerButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				((HomeActivity) getActivity()).playMovie((int) mMovieId, R.string.preview);
			}
		});
        
        mMobileScreenshotImageView = (ImageView) rootView.findViewById(R.id.screen_shot);
        mMobileScreenshotImageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				final Intent intent = new Intent(getActivity(), MovieDetailActivity.class);
	    		intent.putExtra(MovieDetailActivity.EXTRA_MOVIE_ID, mMovieId);
	            startActivity(intent);
			}
		});

        return rootView;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (HomeActivity.class.isInstance(getActivity())) {
        	((HomeActivity) getActivity()).getLatestMoviesCoverFetcher().loadImage(mMovieCoverUrl, mMovieCoverImageView);
        	((HomeActivity) getActivity()).getLatestMoviesScreenshotFetcher().loadImage(mMobileScreenshotUrl, mMobileScreenshotImageView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMovieCoverImageView != null) {
            // Cancel any pending image work
            ImageWorker.cancelWork(mMovieCoverImageView);
            mMovieCoverImageView.setImageDrawable(null);
        }
        if (mMobileScreenshotImageView != null) {
            // Cancel any pending image work
            ImageWorker.cancelWork(mMobileScreenshotImageView);
            mMobileScreenshotImageView.setImageDrawable(null);
        }
    }
}
